package cn.sinata.wanglin.json;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.xilada.xldutils.utils.Utils;

import java.io.IOException;
import java.lang.reflect.Type;

import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 *
 * Created by liaoxiang on 16/6/21.
 */
public class JsonResponseBodyConverterNoDes<T> implements Converter<ResponseBody, T> {
    private final Gson mGson;//gson对象
    private final TypeAdapter<T> adapter;

    /**
     * 构造器
     */
    public JsonResponseBodyConverterNoDes(Gson gson, TypeAdapter<T> adapter) {
        this.mGson = gson;
        this.adapter = adapter;
    }

    /**
     * 转换
     *
     * @param responseBody
     * @return
     * @throws IOException
     */
    @Override
    public T convert(ResponseBody responseBody) throws IOException {
        String s = responseBody.string();
        Utils.systemErr("-解密前->"+s);

//        JsonReader jsonReader = mGson.newJsonReader(responseBody.charStream());
        try {
            return adapter.fromJson(s);
//            return adapter.read(jsonReader);
        } finally {
            responseBody.close();
        }
    }
}
